package com.xywl.escort.view.evaluate;

import com.xywl.escort.bean.CommentsReq;
import com.xywl.escort.bean.CommentsResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class ProductEvaluatePresenter implements ProductEvaluateContract.Presenter {
    private ProductEvaluateContract.View mView;
    private ApiServer mApiServer;

    @Inject
    ProductEvaluatePresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(ProductEvaluateContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadComments(CommentsReq req) {
        mApiServer.frontComments(req)
                .compose(RxUtil.<CommentsResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<CommentsResp>bindToLife())
                .subscribe(new BaseSubscriber<CommentsResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadCommentsFailure();
                    }

                    @Override
                    public void onNext(CommentsResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.loadCommentsSuccess(resp);
                        } else {
                            mView.loadCommentsFailure();
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("ProductEvaluatePresenter").e("unSubscribe");
        mView = null;
    }
}
